﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using DamageCalculator.Classes;

namespace DamageCalculator.SubForms
{

    public partial class WindowedModeGameOverlay : Form
    {
        #region Form Dragging API Support
        //The SendMessage function sends a message to a window or windows.
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        //ReleaseCapture releases a mouse capture
        [DllImportAttribute("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern bool ReleaseCapture();
        #endregion

        Frm_Main.GraphType GT;
        GameSettings currentSettings;
        DateTime LastXpUpdate;
        Double LastXP = 0;
        List<Character> AddedOrderList;
        Frm_Main Owner;

        public WindowedModeGameOverlay(Frm_Main.GraphType UseGrahtype, GameSettings G, Frm_Main Owner)
        {
            InitializeComponent();
            this.label1.Font = Properties.Settings.Default.OverlayFont;
            this.label1.ForeColor = Properties.Settings.Default.OverlayTextColor;
            this.label1.BackColor = Properties.Settings.Default.OverlayBackgroundColor;
            this.CHRT_DPS_HPS.Height = Properties.Settings.Default.OverlayChartHeight;
            this.CHRT_DPS_HPS.Visible = Properties.Settings.Default.OverlayShowChart;
            this.GT = UseGrahtype;
            switch (this.GT)
            {
                case Frm_Main.GraphType.DmgPie:
                    this.damagePieToolStripMenuItem.Checked = true;
                    break;

                case Frm_Main.GraphType.DPS:
                    this.dPSHPSToolStripMenuItem.Checked = true;
                    break;

                case Frm_Main.GraphType.TotalDmg:
                    this.damagePlotToolStripMenuItem.Checked = true;
                    break;
            }
            this.currentSettings = G;
            this.LastXpUpdate = new DateTime();
            this.SetupGraph();
            this.AddedOrderList = new List<Character>();
            this.Owner = Owner;
            this.discoverableModeToolStripMenuItem.Checked = this.Owner.DiscoverableModeOn;
            if (this.Owner.ViewModeisCurrentEncounter)
                this.currentEncounterToolStripMenuItem.Checked = true;
            else
                this.sessionTotalsToolStripMenuItem.Checked = true;

            this.showGraphToolStripMenuItem.Checked = Properties.Settings.Default.OverlayShowChart;
            this.showTextToolStripMenuItem.Checked = Properties.Settings.Default.OverlayShowText;
        }

        public bool DiscoverableModeOn
        {
            get { return this.discoverableModeToolStripMenuItem.Checked; }
            set { this.discoverableModeToolStripMenuItem.Checked = value; }
        }

        public void ResetSettings()
        {
            this.label1.Font = Properties.Settings.Default.OverlayFont;
            this.label1.ForeColor = Properties.Settings.Default.OverlayTextColor;
            this.label1.BackColor = Properties.Settings.Default.OverlayBackgroundColor;
            this.CHRT_DPS_HPS.Height = Properties.Settings.Default.OverlayChartHeight;
            this.CHRT_DPS_HPS.Visible = Properties.Settings.Default.OverlayShowChart;
            this.showTextToolStripMenuItem.Checked = Properties.Settings.Default.OverlayShowText;
            this.showGraphToolStripMenuItem.Checked = Properties.Settings.Default.OverlayShowChart;
        }

        public void SetGraph(Frm_Main.GraphType UseGraphtype)
        {
            try
            {
                this.GT = UseGraphtype;
                this.SetupGraph();
            }
            catch { }
        }

        private void WindowedModeGameOverlay_MouseDown(object sender, MouseEventArgs e)
        {
            this.moveWindow(sender, e);
        }

        private void moveWindow(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (sender.GetType() == typeof(Label))
                {
                    this.contextMenuStrip1.Show(this.label1, new Point(e.X, e.Y));
                }
                else
                    this.contextMenuStrip1.Show(this.CHRT_DPS_HPS, new Point(e.X, e.Y));
            }

            if (e.Button == System.Windows.Forms.MouseButtons.Left && e.Clicks == 2)
            {
                this.Owner.CopytoClipboard();
            }
            
            else if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(this.Handle, 0xa1, 0x2, 0);

                Properties.Settings.Default.OverlayLastXPos = this.Location.X;
                Properties.Settings.Default.OverlayLastYPos = this.Location.Y;
                Properties.Settings.Default.Save();
            }
        }

        public void updateWindow(List<Character> pChars)
        {
            String temp = "";
            if (this.GT == Frm_Main.GraphType.DPS)
            {
                this.CHRT_DPS_HPS.Series["DPS"].Points.Clear();
                this.CHRT_DPS_HPS.Series["HPS"].Points.Clear();
            }
            else if (this.GT == Frm_Main.GraphType.TotalDmg)
            {
                this.CHRT_DPS_HPS.Series["Damage"].Points.Clear();
                this.CHRT_DPS_HPS.Series["Healing"].Points.Clear();
            }
            else if (this.GT == Frm_Main.GraphType.DmgPie)
            {
                this.CHRT_DPS_HPS.Series["Damage"].Points.Clear();
                this.CHRT_DPS_HPS.Series["Healing"].Points.Clear();
                pChars.Sort();
            }

            double totaldps = 0;
            double xpPerHour = 0;
            double myDPS = 0;
            double myHPS = 0;
            Dictionary<String, List<System.Windows.Forms.DataVisualization.Charting.DataPoint>> DataPoints = new Dictionary<string, List<System.Windows.Forms.DataVisualization.Charting.DataPoint>>();

            foreach (Character C in pChars)
            {
                if (!this.AddedOrderList.Contains(C))
                    this.AddedOrderList.Add(C);

                if (temp != "")
                    temp += "\r\n";
                temp += C.ToString();

                ListViewItem LVI = C.GenerateListViewItem();
                if (Double.Parse(LVI.SubItems[11].Text) > 0d && ((DateTime.Now - this.LastXpUpdate).TotalSeconds > 30 || this.LastXP == 0d))
                {
                    this.LastXpUpdate = DateTime.Now;
                    xpPerHour = Double.Parse(LVI.SubItems[11].Text);
                    this.LastXP = Double.Parse(LVI.SubItems[11].Text);
                }
                else
                    xpPerHour = this.LastXP;


                if (!Double.IsInfinity(Double.Parse(LVI.SubItems[2].Text)))
                    totaldps += Double.Parse(LVI.SubItems[2].Text);

                if ((C.Name == this.currentSettings.PlayerName) && !Double.IsInfinity(Double.Parse(LVI.SubItems[2].Text)))
                    myDPS = Double.Parse(LVI.SubItems[2].Text);

                if ((C.Name == this.currentSettings.PlayerName) && !Double.IsInfinity(Double.Parse(LVI.SubItems[7].Text)))
                    myHPS = Double.Parse(LVI.SubItems[7].Text);

                if (this.GT == Frm_Main.GraphType.DPS)
                {
                    if (!Double.IsInfinity(Double.Parse(LVI.SubItems[2].Text)))
                    {
                        System.Windows.Forms.DataVisualization.Charting.DataPoint dpstemp = new System.Windows.Forms.DataVisualization.Charting.DataPoint(this.CHRT_DPS_HPS.Series["DPS"]);
                        dpstemp.SetValueXY(C.Name, new object[] { Double.Parse(LVI.SubItems[2].Text) });
                        this.CHRT_DPS_HPS.Series["DPS"].Points.Add(dpstemp);
                    }

                    if (!Double.IsInfinity(Double.Parse(LVI.SubItems[7].Text)))
                    {
                        System.Windows.Forms.DataVisualization.Charting.DataPoint hpstemp = new System.Windows.Forms.DataVisualization.Charting.DataPoint(this.CHRT_DPS_HPS.Series["HPS"]);
                        hpstemp.SetValueXY(C.Name, new object[] { Double.Parse(LVI.SubItems[7].Text) });

                        this.CHRT_DPS_HPS.Series["HPS"].Points.Add(hpstemp);
                    }
                }

                else if (this.GT == Frm_Main.GraphType.TotalDmg)
                {
                    if (!Double.IsInfinity(Double.Parse(LVI.SubItems[1].Text)))
                    {
                        System.Windows.Forms.DataVisualization.Charting.DataPoint dpstemp = new System.Windows.Forms.DataVisualization.Charting.DataPoint(this.CHRT_DPS_HPS.Series["Damage"]);
                        dpstemp.SetValueXY(C.Name, new object[] { Double.Parse(LVI.SubItems[1].Text) });
                        this.CHRT_DPS_HPS.Series["Damage"].Points.Add(dpstemp);
                    }

                    if (!Double.IsInfinity(Double.Parse(LVI.SubItems[6].Text)))
                    {
                        System.Windows.Forms.DataVisualization.Charting.DataPoint hpstemp = new System.Windows.Forms.DataVisualization.Charting.DataPoint(this.CHRT_DPS_HPS.Series["Healing"]);
                        hpstemp.SetValueXY(C.Name, new object[] { Double.Parse(LVI.SubItems[6].Text) });

                        this.CHRT_DPS_HPS.Series["Healing"].Points.Add(hpstemp);
                        
                    }
                }

                else if (this.GT == Frm_Main.GraphType.DmgPie)
                {
                    if (!Double.IsInfinity(Double.Parse(LVI.SubItems[1].Text)))
                    {
                        System.Windows.Forms.DataVisualization.Charting.DataPoint dpstemp = new System.Windows.Forms.DataVisualization.Charting.DataPoint(this.CHRT_DPS_HPS.Series["Damage"]);
                        dpstemp.SetValueXY(C.Name, new object[] { Double.Parse(LVI.SubItems[1].Text) });
                        if (!DataPoints.ContainsKey("Damage"))
                            DataPoints.Add("Damage", new List<System.Windows.Forms.DataVisualization.Charting.DataPoint>());

                        //DataPoints["Damage"].Add(dpstemp);
                        //DataPoints["Damage"][DataPoints["Damage"].Count - 1]["PieLabelStyle"] = "Disabled";

                        this.CHRT_DPS_HPS.Series["Damage"].Points.Add(dpstemp);
                        this.CHRT_DPS_HPS.Series["Damage"].Points[this.CHRT_DPS_HPS.Series["Damage"].Points.Count - 1]["PieLabelStyle"] = "Disabled";
                    }

                    if (!Double.IsInfinity(Double.Parse(LVI.SubItems[6].Text)))
                    {
                        System.Windows.Forms.DataVisualization.Charting.DataPoint hpstemp = new System.Windows.Forms.DataVisualization.Charting.DataPoint(this.CHRT_DPS_HPS.Series["Healing"]);
                        hpstemp.SetValueXY(C.Name, new object[] { Double.Parse(LVI.SubItems[6].Text) });

                        this.CHRT_DPS_HPS.Series["Healing"].Points.Add(hpstemp);
                        this.CHRT_DPS_HPS.Series["Healing"].Points[this.CHRT_DPS_HPS.Series["Healing"].Points.Count - 1]["PieLabelStyle"] = "Disabled";
                    }
                }
            }

            this.CHRT_DPS_HPS.Location = new Point(this.CHRT_DPS_HPS.Location.X, this.label1.Location.Y + this.label1.Height + 1);

            if (this.label1.Width < Properties.Settings.Default.OverlayMinimumWidth)
                this.CHRT_DPS_HPS.Width = Properties.Settings.Default.OverlayMinimumWidth;
            else
                this.CHRT_DPS_HPS.Width = this.label1.Width;

            if (temp != "")
                temp += "\r\n";

            String hudstring = "";

            if (Properties.Settings.Default.OverlayDisplayMyDPS)
            {
                if (hudstring != "")
                    hudstring += " | ";

                hudstring += "My DPS: " + myDPS.ToString("0.00");
            }
            if (Properties.Settings.Default.OverlayDisplayMyHPS)
            {
                if (hudstring != "")
                    hudstring += " | ";

                hudstring += "My HPS: " + myHPS.ToString("0.00");
            }
            if (Properties.Settings.Default.OverlayDisplayTeamDPS)
            {
                if (hudstring != "")
                    hudstring += " | ";

                hudstring += "Total DPS: " + totaldps.ToString("0.00");
            }
            if (Properties.Settings.Default.OverlayDisplayXP)
            {
                if (hudstring != "")
                    hudstring += " | ";

                hudstring += "XP/Hr: " + xpPerHour.ToString("0.00");
            }

            if (Properties.Settings.Default.OverlayShowText)
                temp += hudstring;
            else
                temp = hudstring;

            this.label1.Text = temp;
        }

        private void SetupGraph()
        {
            switch (this.GT)
            {
                case Frm_Main.GraphType.DPS:
                    this.CHRT_DPS_HPS.Series.Clear();
                    this.CHRT_DPS_HPS.ChartAreas.Clear();
                    this.CHRT_DPS_HPS.Titles.Clear();
                    this.CHRT_DPS_HPS.Legends[0].Enabled = true;
                    this.CHRT_DPS_HPS.Legends[0].Font = new Font(this.CHRT_DPS_HPS.Legends[0].Font.FontFamily, 5);

                    System.Windows.Forms.DataVisualization.Charting.ChartArea C1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea("C1");
                    C1.AxisY.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.False;
                    this.CHRT_DPS_HPS.ChartAreas.Add(C1);

                    System.Windows.Forms.DataVisualization.Charting.Series s1 = new System.Windows.Forms.DataVisualization.Charting.Series("DPS");
                    s1.Color = System.Drawing.Color.Red;
                    s1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
                    s1.ChartArea = this.CHRT_DPS_HPS.ChartAreas[0].Name;
                    s1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;
                    s1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                    s1.Name = "DPS";
                    //chart1.Series[0]["PointWidth"] = "1";
                    s1["PointWidth"] = "1";
                    this.CHRT_DPS_HPS.Series.Add(s1);

                    System.Windows.Forms.DataVisualization.Charting.Series s2 = new System.Windows.Forms.DataVisualization.Charting.Series("HPS");
                    s2.Color = System.Drawing.Color.Blue;
                    s2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
                    s2.ChartArea = this.CHRT_DPS_HPS.ChartAreas[0].Name;
                    s2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;
                    s2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                    s2.Name = "HPS";
                    s2["PointWidth"] = "1";
                    this.CHRT_DPS_HPS.Series.Add(s2);

                    break;

                case Frm_Main.GraphType.TotalDmg:
                    this.CHRT_DPS_HPS.Series.Clear();
                    this.CHRT_DPS_HPS.ChartAreas.Clear();
                    this.CHRT_DPS_HPS.Titles.Clear();
                    this.CHRT_DPS_HPS.Legends[0].Enabled = true;
                    this.CHRT_DPS_HPS.Legends[0].Font = new Font(this.CHRT_DPS_HPS.Legends[0].Font.FontFamily, 5);

                    System.Windows.Forms.DataVisualization.Charting.ChartArea C2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea("C2");
                    C2.AxisY.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.False;
                    this.CHRT_DPS_HPS.ChartAreas.Add(C2);

                    System.Windows.Forms.DataVisualization.Charting.Series s3 = new System.Windows.Forms.DataVisualization.Charting.Series("Damage");
                    s3.Color = System.Drawing.Color.Red;
                    s3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
                    s3.ChartArea = this.CHRT_DPS_HPS.ChartAreas[0].Name;
                    s3.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;
                    s3.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                    s3.Name = "Damage";
                    s3["PointWidth"] = "1";
                    this.CHRT_DPS_HPS.Series.Add(s3);

                    System.Windows.Forms.DataVisualization.Charting.Series s4 = new System.Windows.Forms.DataVisualization.Charting.Series("Healing");
                    s4.Color = System.Drawing.Color.Blue;
                    s4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
                    s4.ChartArea = this.CHRT_DPS_HPS.ChartAreas[0].Name;
                    s4.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;
                    s4.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                    s4.Name = "Healing";
                    s4["PointWidth"] = "1";
                    this.CHRT_DPS_HPS.Series.Add(s4);

                    break;

                case Frm_Main.GraphType.DmgPie:
                    this.CHRT_DPS_HPS.Series.Clear();
                    this.CHRT_DPS_HPS.ChartAreas.Clear();
                    this.CHRT_DPS_HPS.Titles.Clear();
                    this.CHRT_DPS_HPS.Legends[0].Enabled = true;
                    if (this.CHRT_DPS_HPS.Legends.Count < 2)
                    {
                        this.CHRT_DPS_HPS.Legends.Add("l2");
                        this.CHRT_DPS_HPS.Legends[1].Enabled = false;
                    }
                    this.CHRT_DPS_HPS.Legends[0].Font = new Font(this.CHRT_DPS_HPS.Legends[0].Font.FontFamily, 6);

                    //Add Chart Areas
                    System.Windows.Forms.DataVisualization.Charting.ChartArea cDmg = new System.Windows.Forms.DataVisualization.Charting.ChartArea("Damage");
                    cDmg.Position.Auto = false;
                    cDmg.Position.X = 0;
                    cDmg.Position.Y = 0;
                    cDmg.Position.Height = 100;
                    cDmg.Position.Width = 30;
                    this.CHRT_DPS_HPS.ChartAreas.Add(cDmg);

                    System.Windows.Forms.DataVisualization.Charting.ChartArea cHeal = new System.Windows.Forms.DataVisualization.Charting.ChartArea("Healing");
                    cHeal.Position.Auto = false;
                    cHeal.Position.X = 31;
                    cHeal.Position.Y = 0;
                    cHeal.Position.Height = 100;
                    cHeal.Position.Width = 30;
                    
                    this.CHRT_DPS_HPS.ChartAreas.Add(cHeal);

                    //Add Chart Titles
                    System.Windows.Forms.DataVisualization.Charting.Title tDmg = new System.Windows.Forms.DataVisualization.Charting.Title("Damage", System.Windows.Forms.DataVisualization.Charting.Docking.Top);
                    tDmg.DockedToChartArea = "Damage";
                    this.CHRT_DPS_HPS.Titles.Add(tDmg);

                    System.Windows.Forms.DataVisualization.Charting.Title tHeal = new System.Windows.Forms.DataVisualization.Charting.Title("Healing", System.Windows.Forms.DataVisualization.Charting.Docking.Top);
                    tHeal.DockedToChartArea = "Healing";
                    this.CHRT_DPS_HPS.Titles.Add(tHeal);

                    //Add Series
                    System.Windows.Forms.DataVisualization.Charting.Series sDmg = new System.Windows.Forms.DataVisualization.Charting.Series("Damage");
                    sDmg.ChartArea = "Damage";
                    sDmg.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
                    sDmg.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;
                    sDmg.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                    this.CHRT_DPS_HPS.Series.Add(sDmg);

                    System.Windows.Forms.DataVisualization.Charting.Series sHeal = new System.Windows.Forms.DataVisualization.Charting.Series("Healing");
                    sHeal.ChartArea = "Healing";
                    sHeal.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
                    sHeal.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.String;
                    sHeal.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
                    sHeal.Legend = "l2";
                    this.CHRT_DPS_HPS.Series.Add(sHeal);

                    break;
            }
        }

        private void copyStatsToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Owner.CopytoClipboard();
        }

        private void discoverableModeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Owner.SetDiscoverableMode();
        }

        private void addCharacterToWatchListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddCharacterDialog ACD = new AddCharacterDialog(this.Owner.CharsList);

            DialogResult D = ACD.ShowDialog();

            if (D == System.Windows.Forms.DialogResult.OK)
                this.Owner.AddChar(ACD);
        }

        private void closeOverlayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Owner.OverlayClosedByOverlay();
            this.Close();
        }

        private void SwitchGraph(object sender, EventArgs e)
        {
            ToolStripMenuItem T = sender as ToolStripMenuItem;

            foreach (ToolStripMenuItem temp in this.graphTypeToolStripMenuItem.DropDownItems)
            {
                if (temp.Text == T.Text)
                    temp.Checked = true;
                else
                    temp.Checked = false;

                this.Owner.OverlaySwitchedGraph(T.Text);
            }
        }

        private void ChangeView(object sender, EventArgs e)
        {
            ToolStripMenuItem temp = sender as ToolStripMenuItem;

            foreach (ToolStripMenuItem t in this.viewToolStripMenuItem.DropDownItems)
            {
                if (t.Text == temp.Text)
                    t.Checked = true;
                else
                    t.Checked = false;
            }

            if (temp.Text.Contains("Session"))
            {
                this.Owner.ViewModeisSessionTotals = true;
                this.Owner.ViewModeisCurrentEncounter = false;
            }

            else
            {
                this.Owner.ViewModeisSessionTotals = false;
                this.Owner.ViewModeisCurrentEncounter = true;
            }
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Owner.resetReadingToolStripMenuItem_Click(null, null);
        }

        private void showGraphToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.OverlayShowChart = this.showGraphToolStripMenuItem.Checked;
            Properties.Settings.Default.Save();
            this.ResetSettings();
        }

        private void showTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.OverlayShowText = this.showTextToolStripMenuItem.Checked;
            Properties.Settings.Default.Save();
            this.ResetSettings();
        }

        private void nameCurrentEncounterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EncounterNamer EN = new EncounterNamer();

            DialogResult DR = EN.ShowDialog();

            if (DR == System.Windows.Forms.DialogResult.OK)
                this.Owner.NamecurrentEncounter(EN.Name);
        }
    }
}
